package day_2022_9_to_12.Day;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author haomin
 * @date 2022/09/21 22:51
 **/
public class Day19_5_Num107_LevelOrderBottom {
    class Solution {
        LinkedList<List<Integer>> res = new LinkedList<>();
        public List<List<Integer>> levelOrderBottom(TreeNode root) {
            if(root == null) return res;
            Deque<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            while(!queue.isEmpty()){
                int size = queue.size();
                List<Integer> ret = new ArrayList<>();
                for(int i = 0; i < size; ++i){
                    TreeNode cur = queue.poll();
                    ret.add(cur.val);
                    if(cur.left != null) queue.offer(cur.left);
                    if(cur.right != null) queue.offer(cur.right);
                }
                res.addFirst(new ArrayList(ret));
            }
            return res;
        }
    }
}